package com.topisv.tms.repository.task;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.task.MdSendOutTask;

public interface MdSendOutTaskDao extends PagingAndSortingRepository<MdSendOutTask, Long>, JpaSpecificationExecutor<MdSendOutTask> {
	
	/**
	 * 更新作业的数量
	 * @param customerOrderNo
	 * @return int
	 */
	@Modifying
	@Query("update MdSendOutTask set doneNum=doneNum+?2 where customerOrderNo=?1 and doneNum <> workNum")
	int updateDoneNum(String customerOrderNo,int num);
	
	/**
	 * 删除任务
	 * @param customerOrderNo
	 * @return int
	 */
	@Modifying
	@Query("delete from MdSendOutTask where customerOrderNo=?1")
	int deleteTaskByCusOrderNo(String customerOrderNo);
	
	/**
	 * 根据ID更新状态
	 * @param dealStatus
	 * @param ids
	 */
	@Modifying
	@Query("update MdSendOutTask set dealStatus=?1,dealTime=?2 where id in ?3")
	void updateDealStatusByID(String dealStatus,Date date,List<Long> ids);
	
	/**
	 * 根据IP查找
	 * @param ip
	 * @return List<MdSendOutTask>
	 */
	@Query("select t from MdSendOutTask t where dealIp = ?1 and dealStatus=1")
	List<MdSendOutTask> findTask(String ip);
	
	/**
	 * 更新为已加载
	 * @param ip
	 * @param ids
	 * @return List<DispatchOrderTask>
	 */
	@Modifying
	@Query("update MdSendOutTask t set t.dealStatus=2 where dealIp = ?1 and dealStatus=1 and id in ?2")
	void update(String ip,List<Long> ids);
	
	MdSendOutTask findByCustomerOrderNo(String customerOrderNo);
}
